import matplotlib.pyplot as plt
import re
import sys
import numpy as np

log_path = '../_save/on_man_woman_20211007Thu1122_lambda_coord_5/log.txt'

regexp4epoch = re.compile(r'\=+validate epoch (\d+)\=+')
regexp = re.compile(r'val avg loss = (\d+\.?\d*)')

epoch_his = []
cost_his = []
with open(log_path, 'r') as f:
    lines = f.readlines()
    for line in lines:

        # read epoch number
        matcher = regexp4epoch.search(line)
        if matcher is not None:
            number_str = matcher[1]
            try:
                epoch = int(number_str)
                epoch_his.append(epoch)
            except:
                print(f'Error to parse {number_str} to int.', file=sys.stderr)

        # read val loss
        matcher = regexp.search(line)
        if matcher is not None:
            number_str = matcher[1]
            try:
                cost = float(number_str)
                cost_his.append(cost)
            except:
                print(f'Error to parse {number_str} to float.', file=sys.stderr)

epoch_his = np.array(epoch_his)
cost_his = np.array(cost_his)
epoch_his = epoch_his[-len(cost_his):]

plt.figure(figsize=[6, 6])
# plt.plot(cost_his[-500:])
plt.plot(epoch_his, cost_his)
plt.show()
